package com.tt.aile.item.controller;


import com.sun.org.glassfish.gmbal.ParameterNames;
import com.tt.aile.item.pojo.Category;
import com.tt.aile.item.service.CategoryService;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author 86176 shkstrat
 * @date 2020/9/20 - 20:47
 */
@Controller
@RequestMapping("category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    //查询分类
    @GetMapping("list")
    public ResponseEntity<List<Category>> queryCategoriesByPid(@RequestParam("pid") Long pid) {

            if ( pid == null || pid < 0) {
                //响应400
//                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
//                return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
                return ResponseEntity.badRequest().build();
            }
                       //执行查询，获取结果
            List<Category> categories =  this.categoryService.queryCategoriesByPid(pid);
            if(CollectionUtils.isEmpty(categories)){

                //响应404
                //return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
                //return ResponseEntity<>(HttpStatus.NOT_FOUND);
                return ResponseEntity.notFound().build();

            }
            //响应200
            return  ResponseEntity.ok(categories);

    }

    //根据
    @GetMapping
    public ResponseEntity<List<String>> queryNamesByIds(@RequestParam("ids")List<Long> ids){

        List<String> strings = this.categoryService.queryNamesByIds(ids);
        if(CollectionUtils.isEmpty(strings)){

            //响应404
            return ResponseEntity.notFound().build();

        }
        //响应200
        return  ResponseEntity.ok(strings);

    }
    //根据
    @GetMapping("id/{id}")
    public ResponseEntity<String> queryNameById(@PathVariable("id") Long id){

        String name = this.categoryService.queryNameById(id);
        if(name ==null){

            //响应404
            return ResponseEntity.notFound().build();

        }
        //响应200
        return  ResponseEntity.ok(name);

    }

}
